package io.renren.modules.sys.lucene.controller;

import io.renren.modules.sys.lucene.entity.User;
import io.renren.modules.sys.lucene.utils.LuceneUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
//import org.apache.lucene.queryParser.MultiFieldQueryParser;
//import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.apache.lucene.search.BooleanClause.Occur;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import static io.renren.modules.sys.lucene.utils.LuceneUtils.getAnalyzer;

public class test {
//@Test
    public void createIndexDB() throws Exception {
        //把数据填充到JavaBean对象中
        User user1 = new User("1", "孙圣诞节快乐大姐夫", "大家反馈家时代峻峰金坷垃束带结发1");

        User user2 = new User("2", "孙的角度讲", "未来的程序员1");
        User user3 = new User("3", "肯德基副科级", "第三方科了乐技的撒放假");
        User user4 = new User("4", "都是解放军", "适得府君书打开九分裤");
        User user5 = new User("5", "的康师傅", "我看空了点啥符号挥洒的回复");
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
    List<Document> documents =new ArrayList<>();
        for (User us:list) {
            Document document = LuceneUtils.javaBean2Document(us);
            documents.add(document);
        }

        /**
         * IndexWriter将我们的document对象写到硬盘中
         *
         * 参数一：Directory d,写到硬盘中的目录路径是什么
         * 参数二：Analyzer a, 以何种算法来对document中的原始记录表数据进行拆分成词汇表
         * 参数三：MaxFieldLength mfl 最多将文本拆分出多少个词汇
         *
         * */
        IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_43,
                LuceneUtils.getAnalyzer());;
        //IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), getAnalyzer(), LuceneUtils.getMaxFieldLength());
           IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), iwConfig);
        //将Document对象通过IndexWriter对象写入索引库中
       for (Document ss:documents)
            indexWriter.addDocument(ss);
        //关闭IndexWriter对象
    //索引库优化
      /*  indexWriter.optimize();

    //设置合并因子为3，每当有3个cfs文件，就合并
        indexWriter.setMergeFactor(3);*/
        indexWriter.close();
    }
   // @Test
    public void findIndexDB() throws Exception {
        //创建IndexSearcher对象
        IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(LuceneUtils.getDirectory()));
        //创建QueryParser对象
        String[]stringQuery={"让泡泡","资源等"};
        //待查找字符串对应的字段
        String[] fields={"name","sammary"};
        //Occur.MUST表示对应字段必须有查询值， Occur.MUST_NOT 表示对应字段必须没有查询值，Occur.SHOULD表示对应字段应该存在查询值（但不是必须）
        Occur[]occ={Occur.SHOULD, Occur.SHOULD};
        Query query = MultiFieldQueryParser.parse(Version.LUCENE_43,stringQuery,fields,occ,LuceneUtils.getAnalyzer());
        TopDocs topDocs = indexSearcher.search(query,5);
        System.out.println("命中数:"+topDocs.totalHits);

        //QueryParser queryParser = new QueryParser(Version.LUCENE_30, "userName", LuceneUtils.getAnalyzer());
        //给出要查询的关键字
       // String keyWords = "孙";
        //创建Query对象来封装关键字
        //Query query = queryParser.parse(keyWords);
        //用IndexSearcher对象去索引库中查询符合条件的前100条记录，不足100条记录的以实际为准
        //TopDocs topDocs = indexSearcher.search(query, 100);
        //获取符合条件的编号
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            int no = scoreDoc.doc;
            //用indexSearcher对象去索引库中查询编号对应的Document对象
            Document document = indexSearcher.doc(no);
            //将Document对象中的所有属性取出，再封装回JavaBean对象中去
            User user = (User) LuceneUtils.Document2JavaBean(document, User.class);
            System.out.println(user);

        }
    }

}
